package ar.uba.dc.cmovil.tripplanner.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import ar.uba.dc.cmovil.tripplanner.domain.Place;
import ar.uba.dc.cmovil.tripplanner.services.HttpTaskHandler;
import ar.uba.dc.cmovil.tripplanner.services.WikipediaService;
import ar.uba.dc.cmovil.tripplanner.utils.DateFormatter;
import ar.uba.dc.cmovil.tripplanner.utils.PlaceUtils;
import ar.uba.dc.cmovil.tripplanner.view.PlaceView;

public class PlaceActivity extends FragmentActivity {

    public static final String SELECTED_PLACE = "selected_place";
    private PlaceView view;
    private Place place;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StrictMode.ThreadPolicy
                policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Intent intent = getIntent();
        place = (Place) intent.getSerializableExtra(SELECTED_PLACE);

        view = new PlaceView(this);
        view.setPlaceName(place.getName());
        view.setArrivalDate(DateFormatter.formatDate(place.getArrivalDate()));
        view.getAddToCalendarButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlaceUtils.addToCalendar(PlaceActivity.this, place);
            }
        });

        loadWikipediaInfo(place.getName());

        setTitle(place.getName());
        setUpMapIfNeeded();
    }

    private void loadWikipediaInfo(final String name) {
        final WikipediaService wiki = WikipediaService.instance();
        wiki.getDescriptionFor(name, new HttpTaskHandler<String>() {
            @Override
            public void onSuccess(String result) {
                view.setPlaceDescription(result);
                final String url = wiki.getWikiLink(name);
                view.setMoreDescriptionLink(url);
                view.getMoreDescription().setVisibility(View.VISIBLE);
                view.getMoreDescription().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        PlaceActivity.this.startActivity(intent);
                    }
                });
            }

            @Override
            public void onFail() {
                view.getMoreDescription().setVisibility(View.INVISIBLE);
            }
        });
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (this.view.getMap() == null && place.getLocation() != null) {
            // Try yo obtain the map from the SupportMapFragment.
            GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(this.view.getMapId())).getMap();

            // Check if we were successful in obtaining the map.
            if (map != null) {
                setUpMap(map);
                this.view.setMap(map);
            }
        }
    }

    private void setUpMap(GoogleMap map) {
        LatLng latlng;
        if (place.getLocation() == null || place.getLocation().getLatitude() == null || place.getLocation().getLongitude() == null) {
            latlng = new LatLng(.0, .0);
        } else {
            double latitude = place.getLocation().getLatitude().getValue();
            double longitude = place.getLocation().getLongitude().getValue();
            latlng = new LatLng(latitude, longitude);
        }
        map.addMarker(new MarkerOptions().position(latlng).title(place.getName()));
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latlng, 10);
        map.animateCamera(cameraUpdate);
    }
}
